--package.path  = "../weibo/?.lua;weibo/?.lua;" .. package.path
--package.cpath = "../weibo/?.lua;weibo/?.lua;" .. package.cpath
local ngx   = ngx
local base  = _G
local string = string
local util  = require "weibo.util" 
local cjson = require "cjson"
module("weibo")


local page_size = 30

local function _split(str,delim,_t)
  local i,j,k
  local t = ((_t) and _t) or {}
  k = 1
  while true do
     i,j = base.string.find(str,delim,k)
     if i == nil then
        base.table.insert(t,base.string.sub(str,k))
        return t
     end
     base.table.insert(t,base.string.sub(str,k,i - 1))
     k = j + 1
  end
end




function idol()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']  
 local t    = args['t']  

 local r,err = util.redis_eval("return weibo_redis.idol(ARGV[1],ARGV[2])"
                                ,0,uid,t);
 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.idol error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end


function myidols()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']  
 local start  = args['start']
 local len    = args['len']
       start  = ((not start) and 0) or start
       len  = ((not len) and page_size) or len



 local r,err = util.redis_eval(
               "return weibo_redis.myidols(ARGV[1],ARGV[2],ARGV[3])"
               ,0,uid,start,len)
 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.myidols error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end



function publish()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']
 local cont = args['cont']  
 local src  = args['src']  
 local _type = args['type']   -- 0:weibo 1:forward  
 local tid = args['tid']   -- 0:weibo 1:forward  
 --local state = 0  -- 0:start 1:transfer 2:published
 --local ctime = args['ctime']
 --local _type = args['type']  

 local wid = util.getUUID()
 ngx.log(ngx.DEBUG,"wid:" .. wid)
 if wid == -1 then  res.code = -1 res.data = "UUID error"  return res end

 args["wid"] = wid
 local args_str = util.table_tostring(args);
 ngx.log(ngx.DEBUG,"#publish args_str:" .. args_str)

 local r,err = util.redis_eval(
               string.format("return weibo_redis.publish(%s)",args_str)
                              ,0);
 --ngx.log(ngx.DEBUG,"#publish eval res:" .. r .. "err:" .. err)
 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.publish error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end


function selfWeiboCnt()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']

 if not uid then res.code = -1 res.data = "invalid param not uid" return res end

 local r,err = util.redis_eval(
               "return weibo_redis.selfWeiboCnt(ARGV[1])",
               0,uid)

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.selfWeiboCnt error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end

function selfWeibo()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']
 local start  = args['start']
 local len    = args['len']
       start  = ((not start) and 0) or start
       len  = ((not len) and page_size) or len

 local r,err = util.redis_eval(
               "return weibo_redis.selfWeibo(ARGV[1],ARGV[2],ARGV[3])",
               0,uid,start,len)

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.selfWeibo error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end


function idolWeibo()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']
 local start  = args['start']
 local len    = args['len']
       start  = ((not start) and 0) or start
       len  = ((not len) and page_size) or len

 local r,err = util.redis_eval(
               "return weibo_redis.idolWeibo(ARGV[1],ARGV[2],ARGV[3])",
               0,uid,start,len)

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.idolWeibo error: " .. err
    return res
 end

 local node = {['path']="unix:/tmp/redis.sock"}
 
 r,err = util.redis_eval(node,
               "return weibo_redis.getWeiboes(ARGV[1])",
               0,base.table.concat(r,","))

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.getWeiboes error: " .. err
    return res
 end

 local data = {} 

 for i,v in base.ipairs(r) do
   ---ngx.log(ngx.DEBUG,"temp res:" .. v);
   local entity = {} 
   for ii=1,#v,2 do
     entity[v[ii]] = v[ii+1]
   end
   base.table.insert(data,entity)
 end



 res.data = data;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end

function idolWeiboIds()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local uid  = args['uid']
 local start  = args['start']
 local len    = args['len']
       start  = ((not start) and 0) or start
       len  = ((not len) and page_size) or len

 local r,err = util.redis_eval(
               "return weibo_redis.idolWeibo(ARGV[1],ARGV[2],ARGV[3])",
               0,uid,start,len)

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.idolWeibo error: " .. err
    return res
 end

 res.data = r;

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end


function getWeiboes()
 local res  = {code=0,data="success"};
 local args = ngx.req.get_uri_args()
 local wids  = args['wids']
 local wid   = args['wid']
 local param = {}

 if wid and wid ~= "" then
    if base.type(wid) == "table" then
       param = wid
    else
       base.table.insert(param,wid)
    end 
 end

 if wids and wids ~= "" then
    _split(wids,",",param)
 end

 if base.table.getn(param) == 0 then 
   ngx.log(ngx.ERR,"#getWeiboes invalid parameter")
   res.code = -1
   res.data = "weibo_redis.getWeiboes invalid param"
   return res
 end

 local node = {['path']="unix:/tmp/redis.sock"}
 --local node = {['host']="129.2.0.2",['port']=6379}
 
 local r,err = util.redis_eval(node,
               "return weibo_redis.getWeiboes(ARGV[1])",
               0,base.table.concat(param,","))

 if not r then
    ngx.log(ngx.ERR,"exec_redis error" .. err)
    res.code = -1
    res.data = "weibo_redis.getWeiboes error: " .. err
    return res
 end

 local data = {} 

 for i,v in base.ipairs(r) do
   ---ngx.log(ngx.DEBUG,"temp res:" .. v);
   local entity = {} 
   for ii=1,#v,2 do
     entity[v[ii]] = v[ii+1]
   end
   base.table.insert(data,entity)
 end


 res.data = data

 ngx.log(ngx.DEBUG,"res:" .. cjson.encode(res));
 return res
end
